package com.huawei.cloud.phone.management.common;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class BeanFactory implements ApplicationContextAware, BeanPostProcessor {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
        BeanFactory.context = applicationContext;
    }

    /**
     * 获取Spring管理的Bean对象
     *
     * @param beanClass 要获取的对象类型
     * @param <T>       对象类型
     * @return Bean对象
     */
    public static <T> T getBean(Class<T> beanClass) {
        return Objects.requireNonNull(context).getBean(beanClass);
    }
}
